import 'package:shared_preferences/shared_preferences.dart';

class LocalStorage {
  // 存
  static void setItem(String key, value) async {
    var localStorage = await SharedPreferences.getInstance();
    // int类型
    if (value is int) {
      localStorage.setInt(key, value);
    }
    // 字符串类型
    if (value is String) {
      localStorage.setString(key, value);
    }
    // List String
    if (value is List) {
      localStorage.setStringList(key, value.cast<String>());
    }
    // 布尔
    if (value is bool) {
      localStorage.setBool(key, value);
    }
    // double
    if (value is double) {
      localStorage.setDouble(key, value);
    }
  }

  // 获取指定
  static getItem(String key) async {
    var localStorage = await SharedPreferences.getInstance();
    return localStorage.get(key);
  }

  // 清除指定
  static removeItem(String key) async {
    var localStorage = await SharedPreferences.getInstance();
    return localStorage.remove(key);
  }

  // 清除所有
  static clear() async {
    var localStorage = await SharedPreferences.getInstance();
    localStorage.clear();
  }
}
